import layoutHeaderAside from '@/layout/header-aside'

// 由于懒加载页面太多的话会造成webpack热更新太慢，所以开发环境不使用懒加载，只有生产环境使用懒加载
const _import = require('@/libs/util.import.' + process.env.NODE_ENV)

/**
 * 在主框架内显示
 */
const frameIn = [
  {
    path: '/',
    redirect: {name: 'index'},
    component: layoutHeaderAside,
    children: [
      // 首页
      {
        path: 'index',
        name: 'index',
        meta: {
          auth: true
        },
        component: _import('system/index')
      },
      // 演示页面
      {
        path: 'page1',
        name: 'page1',
        meta: {
          title: '页面 1',
          auth: true
        },
        component: _import('demo/page1')
      },
      {
        path: 'page2',
        name: 'page2',
        meta: {
          title: '页面 2',
          auth: true
        },
        component: _import('demo/page2')
      },
      {
        path: 'page3',
        name: 'page3',
        meta: {
          title: '页面 3',
          auth: true
        },
        component: _import('demo/page3')
      },
      // 系统 前端日志
      {
        path: 'log',
        name: 'log',
        meta: {
          title: '前端日志',
          auth: true
        },
        component: _import('system/log')
      },
      // 刷新页面 必须保留
      {
        path: 'refresh',
        name: 'refresh',
        hidden: true,
        component: _import('system/function/refresh')
      },
      // 页面重定向 必须保留
      {
        path: 'redirect/:route*',
        name: 'redirect',
        hidden: true,
        component: _import('system/function/redirect')
      }
    ]
  }
]

const innerFiles = require.context('../views/inner', true, /\.vue$/)
let menuJson = {}
innerFiles.keys().forEach(path => {
  let routeDefault = innerFiles(path).default

  let fileUrl = path.replace(/\.\//g, '')
  let paths = fileUrl.split('/')
  if (paths.length === 2) {
    //加载固定二级目录结构文件
    let names = paths[1].split('.')
    let realPath = paths[0] + '/' + names[0]

    let item = {
      path: realPath,
      name: realPath,
      meta: {
        title: routeDefault.title != undefined ? routeDefault.title : names[0],
        auth: true
      },
      component: _import('inner/' + realPath)
    }

    var probablyTitle = ''
    var probablyIcon = ''
    var isIndex = false
    if (names[0] == 'index') {
      probablyTitle = routeDefault.title == undefined ? '' : routeDefault.title
      probablyIcon = routeDefault.icon == undefined ? '' : routeDefault.icon
      isIndex = true
    }
    if (!menuJson[paths[0]]) {
      menuJson[paths[0]] = {
        title: probablyTitle,
        icon: probablyIcon,
        children:[]
      }
    }
    if(isIndex){
      menuJson[paths[0]].title = probablyTitle
      menuJson[paths[0]].icon = probablyIcon
    } else {
      menuJson[paths[0]].children.push({
        path:realPath,
        title: routeDefault.title != undefined ? routeDefault.title : names[0]
      })
    }
    frameIn[0].children.push(item)
  }
})
let menuArray = []
for(var j in menuJson){
  menuArray.push(menuJson[j])
}
export const menuResources = menuArray
/**
 * 在主框架之外显示
 */
const frameOut = [
  // 登录
  {
    path: '/login',
    name: 'login',
    component: _import('system/login')
  }
]

/**
 * 错误页面
 */
const errorPage = [
  {
    path: '*',
    name: '404',
    component: _import('system/error/404')
  }
]

// 导出需要显示菜单的
export const frameInRoutes = frameIn

// 重新组织后导出
export default [
  ...frameIn,
  ...frameOut,
  ...errorPage
]
